package logger

import (
	"fmt"
	"time"
)

type ConsoleLogger struct {
	level      LogLevel
	Method     string
	Path       string
	RemoteAddr string
	Status     int
}

func NewConsoleLogger(level string) (ConsoleLogger, error) {
	logLevel, err := parseLogLevel(level)
	return ConsoleLogger{
		level: logLevel,
	}, err
}

func (c ConsoleLogger) enable(level LogLevel) bool {
	return c.level <= level
}

func (c ConsoleLogger) log(level LogLevel, format string, args ...interface{}) {
	if c.enable(level) {
		var msg string
		if len(args) != 0 {
			msg = fmt.Sprintf(format, args...)
		} else {
			msg = fmt.Sprintf(format)
		}

		//funcName, fileName, lineNo := getInfo(3)
		now := time.Now().Format("2006-01-02 15:04:05")
		lvStr := getLogString(level)
		fmt.Printf("[%s] [%s] [%s] [%s]  %s  [%d]  %s \n", now, lvStr, c.RemoteAddr, c.Method, c.Path, c.Status, msg)
	}
}

// Debug ...
func (c ConsoleLogger) Debug(format string, args ...interface{}) {
	c.log(DEBUG, format, args...)
}

// Info ...
func (c ConsoleLogger) Info(format string, args ...interface{}) {
	c.log(INFO, format, args...)
}

// Warning ...
func (c ConsoleLogger) Warning(format string, args ...interface{}) {
	c.log(WARNING, format, args...)
}

// Error ...
func (c ConsoleLogger) Error(format string, args ...interface{}) {
	c.log(ERROR, format, args...)

}

// Fatal ...
func (c ConsoleLogger) Fatal(format string, args ...interface{}) {
	c.log(FATAL, format, args...)
}
